﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class StudentBLL
    {
        public static IQueryable<StudentInfo> GetList()
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                var re = dal.SelectQueryable(o => true);
                return re;
            }
        }
        public static IQueryable<StudentInfo> GetList(int collegeId, int subjectId, int classId, string uid, string name)
        {
            var re = GetList();
            if (collegeId != 0)
                re = re.Where(o => o.SubjectInfo.CollegeId == collegeId);
            if (subjectId != 0)
                re = re.Where(o => o.SubjectId == subjectId);
            if (classId != 0)
                re = re.Where(o => o.Class == classId);
            if (!string.IsNullOrEmpty(uid))
                re = re.Where(o => o.Uid.Contains(uid));
            if (!string.IsNullOrEmpty(name))
                re = re.Where(o => o.StuName.Contains(name));
            return re;
        }

        public static StudentInfo Get(int id)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static bool Edit(StudentInfo info)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Update(info) != null;
            }
        }

        public static bool Add(StudentInfo info)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Add(info) != null;
            }
        }

        public static bool Add(IEnumerable<StudentInfo> list)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Add(list) != null;
            }
        }

        public static bool IsExist(string uid, int id = 0)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Id != id && o.Uid == uid) != null;
            }
        }

        public static StudentInfo Login(string uid, string pass)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Uid == uid && o.PassWord == pass);
            }
        }

        public static bool Del(string[] ids)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Delete(o => ids.Contains(o.Id.ToString())) != null;
            }
        }

        public static bool Review(string ids,int type)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Review(ids, type);
            }
        }


    }
}
